package com.net.xpay.partner.controller.partner;

import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.domain.stat.UserSafePayTransMonthStat;
import com.net.xpay.common.manager.stat.UserSafePayTransMonthStatManager;
import com.net.xpay.common.manager.stat.UserTransMonthStatManager;
import com.net.xpay.common.model.UserUnionTransMonthStatResultModel;
import com.net.xpay.common.model.search.UserUnionTransMonthStatSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.partner.secutiry.RequestUser;
import com.net.xpay.partner.vo.UserUnionTransMonthStatVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/partner/user-union-month-trans-stat")
@Api(tags = "合伙人/用户月转账统计", description = "用户月转账统计")
public class UserUnionTransMonthStatController {

    @Autowired
    private UserTransMonthStatManager userTransMonthStatManager;

    @Autowired
    private UserSafePayTransMonthStatManager userSafePayTransMonthStatManager;

    @ApiOperation(value = "用户月转账统计")
    @PostMapping(value = "/search")
    public ResultPagedList<UserUnionTransMonthStatVo> search(@ApiIgnore @RequestUser Partner partner, @RequestBody UserUnionTransMonthStatSearchModel searchModel) {
        searchModel.setPartnerId(partner.getId());
        // 根据查询条件进行查询
        List<UserUnionTransMonthStatResultModel> records = userTransMonthStatManager.searchUnion(searchModel);
        // 查询带条件后数据的条数
        long count = userTransMonthStatManager.searchUnionCount(searchModel);

        List<UserUnionTransMonthStatVo> vos = BeanCopierUtil.batchCopy(UserUnionTransMonthStatResultModel.class, UserUnionTransMonthStatVo.class, records);
        for (UserUnionTransMonthStatVo vo : vos) {
            if (vo.getType() == 2) {
                UserSafePayTransMonthStat monthStat = userSafePayTransMonthStatManager.getById(vo.getId());
                vo.setSuccessBankAmount(monthStat.getSuccessBankAmount());
                vo.setSuccessBankCount(monthStat.getSuccessBankCount());
                vo.setSuccessAlipayAmount(monthStat.getSuccessAlipayAmount());
                vo.setSuccessAlipayCount(monthStat.getSuccessAlipayCount());
            }
        }
        return new ResultPagedList<>(vos, count, searchModel);
    }


}
